﻿using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using FFmpegVideoClip.Lib;
using FFmpegVideoClip.Lib.UtilityControlsTools;

namespace FFmpegVideoClip
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        //protected override void OnStartup(StartupEventArgs e)
        //{
        //    base.OnStartup(e);
        //}
        

        #region TextBox 文本框 启用和关闭编辑功能 基础样式


        private void TextBox_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ((TextBox)sender).IsReadOnly = false;
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TextBox_LostFocus(sender, null!);
                e.Handled = true;
            }

        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            if (textBox.IsReadOnly)
                return;
            textBox.IsReadOnly = true;
        }

        
        #endregion
      

        #region 处理器 文本区 启用和关闭编辑功能-验证处理器

        private void ProcessorFullNameSetComplete_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ProcessorFullNameSetComplete_LostFocus(sender, null!);
                e.Handled = true;
            }
        }

        private void ProcessorFullNameSetComplete_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            if (textBox.IsReadOnly)
                return;
            textBox.IsReadOnly = true;
            if (string.IsNullOrEmpty(textBox.Text))
                return;

            string ProName = textBox.Text;
            string windows = Path.Combine(Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System))!, "Windows");
            if (!Environment.GetEnvironmentVariable("PATH")?.Split(';').Where(p => !p.StartsWith(windows, StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(p) && Directory.Exists(p))
                 .Any(p => Directory.GetFiles(p, "*.exe", SearchOption.AllDirectories)
                 .Any(file => Path.GetFileName(file).Equals(ProName, StringComparison.OrdinalIgnoreCase))) ?? false)
            {
                MessageBoxEx.Show(Application.Current.MainWindow, $"在可查询的环境中未找到[{ProName}]可执行文件,在最终执行前,请确认环境变量或者在程序目录" +
                    $"[{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tools")}]中存在该执行文件,以避免意外", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        #endregion

        #region TextEdit 状态样式

        private void UserTextEditor_OnEditorStateChange(object sender, RoutedEventArgs e)
        {
            UserTextEditor ute= (UserTextEditor)sender;
            if (ute.IsReadOnly)
            {
                ute.SyntaxHighlighting = ConfigHighlightingDefinition.DefaultDefinition;
            }
            else
                ute.SyntaxHighlighting = ConfigHighlightingDefinition.RedDefinition;
        }


        #endregion

        private void BaseButtonStyle_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Tools.CloseKeyInputEditor((UIElement)sender);
        }
    }
}
